﻿@page "/"
@using MauiApp1.Data;
@using System.Text;
@using Plugin.BLE.Abstractions.Contracts;
@inject BleTester MyBleTester

<h3>测试低功耗蓝牙</h3>

<ul class="list-group">
    <li class="list-group-item">
        <button type="button" class="btn btn-primary btn-sm" @attributes=IsScanningCss @onclick=ScanDevice>扫描外设</button>
    </li>
    @if (IsFound)
    {
        <li class="list-group-item">
            <strong>@MyBleTester.TagDeviceInfo</strong>
        </li>
        <li class="list-group-item">
            <buton type="button" class="btn btn-primary btn-sm" @onclick=ConnectDevice>连接外设</buton>
        </li>
        @if (IsConnected)
        {
            <li class="list-group-item">
                <button type="button" class="btn btn-primary btn-sm" @onclick=ShowAll>列出所有的服务下面对应的特征 是否可以读写</button>
            </li>
            @if (allCharacteristicsDic != null)
            {
                foreach (var characteristic in allCharacteristicsDic)
                {
                    <li class="list-group-item">
                        <strong>@characteristic.Key</strong>: @characteristic.Value
                    </li>
                }
            }


            <li class="list-group-item">
                <button type="button" class="btn btn-primary btn-sm" @onclick=ReadDeviceName>读取设备名</button>
            </li>
            <li class="list-group-item">
                <strong>@MyBleTester.ReadDeviceNameResult</strong>
            </li>
            @if (MyBleTester != null && MyBleTester.characteristics != null)
            {
                <li class="list-group-item">
                    @foreach (var characteristic in MyBleTester.characteristics)
                    {
                        <p>@characteristic.Uuid: @characteristic.CanWrite</p>
                    }
                </li>
            }

            <li class="list-group-item">
                <button type="button" class="btn btn-primary btn-sm" @onclick=PrintTestData>打印测试数据</button>
            </li>

        }
    }
</ul>

@code {

    private bool IsScanning = false;
    private bool IsFound = false;
    private bool IsConnected = false;
    private bool IsReadSuccess = false;

    public IReadOnlyList<IService> allService { get; set; } = null;
    public List<ICharacteristic> allCharacteristics { get; set; } = null;
    public Dictionary<string, string> allCharacteristicsDic { get; set; } = null;

    private Dictionary<string, object>? IsScanningCss => IsScanning ? new() { { "disabled", "" }, } : null;

    //扫描外设
    private async void ScanDevice()
    {
        MyBleTester.TagDeviceName = "Printer_6882_BLE";

        IsScanning = true;
        IsFound = false;

        //开始扫描
        IsFound = await MyBleTester.StartScanAsync();

        IsScanning = false;

        //异步更新UI
        await InvokeAsync(() => StateHasChanged());
    }

    //连接外设
    private async void ShowAll()
    {
        allService = await MyBleTester.Device.GetServicesAsync();

        allCharacteristics = new List<ICharacteristic>();
        allCharacteristicsDic = new Dictionary<string, string>();
        foreach (var service in allService)
        {
            var characteristics = await service.GetCharacteristicsAsync();
            foreach (var characteristic in characteristics)
            {
                allCharacteristics.Add(characteristic);
                allCharacteristicsDic.Add(characteristic.Id.ToString(), characteristic.CanWrite.ToString());
            }
        }
        //异步更新UI
        await InvokeAsync(() => StateHasChanged());
    }
    //连接外设
    private async void ConnectDevice()
    {
        //连接外设
        IsConnected = await MyBleTester.ConnectDeviceAsync();

        //异步更新UI
        await InvokeAsync(() => StateHasChanged());
    }

    //读取设备名
    private async void ReadDeviceName()
    {
        //读取设备名
        IsReadSuccess = await MyBleTester.ReadDeviceName();

        //异步更新UI
        await InvokeAsync(() => StateHasChanged());
    }

    //读取设备名
    private async void PrintTestData()
    {
        //发送测试打印数据
        byte[] testData = Encoding.ASCII.GetBytes("SIZE 40 mm,30 mm\nGAP 2 mm,0\nDIRECTION 1\nCLS\nTEXT 100,100,\"3\",0,1,1,\"Hello, world!\"\nPRINT 1\n");
        bool isSent = await MyBleTester.SendDataAsync(allCharacteristics.Where(it => it.Id==Guid.Parse("49535343-8841-43f4-a8d4-ecbe34729bb3")).FirstOrDefault(), testData);

        bool isSent2 = await MyBleTester.SendDataAsync(allCharacteristics.Where(it => it.Id==Guid.Parse("00002af1-0000-1000-8000-00805f9b34fb")).FirstOrDefault(), testData);


        bool isSent3 = await MyBleTester.SendDataAsync(allCharacteristics.Where(it => it.Id==Guid.Parse("bef8d6c9-9c21-4c9e-b632-bd58c1009f9f")).FirstOrDefault(), testData);

    }
}
